package com.haiertvbic.tvnow.util;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.haiertvbic.tvnow.model.ThridProgram;

public class Util {

	public static View setActivityView(Window activity_window,
			WindowManager window_manager) {
		return setActivityView(activity_window, window_manager, 0, 0);
	}

	public static View setActivityView(Window activity_window,
			WindowManager window_manager, int width, int height) {
		View activity_view = activity_window.getDecorView();

		WindowManager.LayoutParams lp = activity_window.getAttributes();
		Display tv_display = window_manager.getDefaultDisplay();
		if (width == 0) {
			lp.width = tv_display.getWidth() / 5;
		} else {
			lp.width = width;
		}
		if (height == 0) {
			lp.height = tv_display.getHeight();
		} else {
			lp.height = height;
		}
		lp.gravity = Gravity.RIGHT | Gravity.CENTER_VERTICAL;
		activity_window.setAttributes(lp);
		return activity_view;
	}

	/**
	 * 判断是电视剧还是不是,根据集数判断
	 * 
	 * @param programJs
	 * @return 若是电视剧返回true
	 */
	public static boolean isJs(ArrayList<ThridProgram> programJs) {
		String isJs = programJs.get(0).getJs();
		if (isJs != null && isJs.trim().length() > 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 开启浏览器意图
	 * 
	 * @param arg2
	 */
	public static void startBrower(String programUrl, Activity context) {
		Intent intent = new Intent("android.intent.action.VIEW");
		Uri uri = Uri.parse(programUrl);
		intent.setDataAndType(uri, "text/html");
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		toOtherApk(context);
		context.startActivity(intent);
	}

	public static void toOtherApk(Activity context) {
		Editor share = context.getSharedPreferences("isLoad",
				Context.MODE_PRIVATE).edit();
		share.putString(Constant.toOtherApk, Constant.toOtherApk);
		share.commit();
	}

	/**
	 * 判断传来的包名有没有装
	 * 
	 * @param packName
	 * @return 若系统内有此apk则返回ture
	 */
	public static boolean checkApk(Context context, String packName) {
		PackageManager pm = context.getPackageManager();
		List<PackageInfo> infoList = pm
				.getInstalledPackages(PackageManager.GET_SERVICES);
		for (PackageInfo info : infoList) {
			if (packName.equals(info.packageName)) {
				return true;
			}
		}
		return false;
	}

	public static void initToOtherApk(Activity context) {
		Editor share = context.getSharedPreferences("isLoad",
				Context.MODE_PRIVATE).edit();
		share.putString(Constant.toOtherApk, "null");
		share.commit();
	}

}
